<?php
	require_once(__DIR__.'/../models/Rating.php');
	require_once (__DIR__.'/LogDao.php');
	
	define("APP_TBL", "rate_app");
	define("RATING_TBL", "rate_rating");
	
	function selectRandomRates($amount){
				
		$statement = "SELECT r.rating as points, r.comment as comment, r.timestamp as timestamp, a.name as app_name
			FROM " . RATING_TBL . " r, " . APP_TBL . " a
			WHERE r.app_id = a.id
			ORDER BY RAND()
			LIMIT " . $amount;
						
		$query = mysql_query($statement);
		
		if(!$query){
			writeError("selectRandomRates: Cannot query the database: " . mysql_error());
		}
		
		$result = array();
		
		while ($row = mysql_fetch_array($query)) {
			$rating = new Rating();
			$rating->points = $row["points"];
			$rating->app_name = $row["app_name"];
			$rating->comment = $row["comment"];
			$rating->timestamp = $row["timestamp"];
			
			$result[] = $rating;
		}
				
		return $result;
	}
	
	function selectRatesAmount(){
	
		$statement = "SELECT count(1) as amount
			FROM " . RATING_TBL;
	
		$query = mysql_query($statement);
	
		if(!$query){
			writeError("selectRatesAmount: Cannot query the database: " . mysql_error());
		}
	
		while ($row = mysql_fetch_array($query)) {
			$rating_amount = $row["amount"];
		}
	
		return $rating_amount;
	}
?>